package utfpr.gang5.hermes.controledepresenca.services.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;

import utfpr.gang5.hermes.controledepresenca.dao.CrudException;
import utfpr.gang5.hermes.controledepresenca.dao.GenericCrud;
import utfpr.gang5.hermes.controledepresenca.pojo.Aluno;
import utfpr.gang5.hermes.controledepresenca.pojo.CalendarioSemanal;
import utfpr.gang5.hermes.controledepresenca.pojo.Disciplina;
import utfpr.gang5.hermes.controledepresenca.services.CalendarioSemanalService;
import utfpr.gang5.hermes.controledepresenca.util.HibernateUtil;

import com.ibm.icu.util.Calendar;

public class HibernateCalendarioSemanalServiceImpl extends GenericCrud<CalendarioSemanal> implements
		CalendarioSemanalService {

	public HibernateCalendarioSemanalServiceImpl() {
	}

	@Override
	public CalendarioSemanal carregar(Long id) throws CrudException {
		return carregar(CalendarioSemanal.class, id);
	}

	@Override
	public List<CalendarioSemanal> carregarTodos() throws CrudException {
		return carregarTodos(CalendarioSemanal.class);
	}

	@Override
	public Disciplina buscarDisciplinaAtual(Aluno aluno) throws CrudException {
		Calendar agora = Calendar.getInstance();
		int week = agora.get(Calendar.DAY_OF_WEEK);
		
		String sem = null;
		if (week == 2)
			sem = CalendarioSemanal.SEG;
		else if (week == 3)
			sem = CalendarioSemanal.TER;
		else if (week == 4)
			sem = CalendarioSemanal.QUA;
		else if (week == 5)
			sem = CalendarioSemanal.QUI;
		else if (week == 6)
			sem = CalendarioSemanal.SEX;
		
		org.hibernate.Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Criteria c = session.createCriteria(CalendarioSemanal.class);
		c.createAlias("disciplina", "disc");
		c.createAlias("disc.curso", "curso");
		c.add(Restrictions.eq("curso.id", aluno.getCurso().getId()));
		c.add(Restrictions.eq("dia", sem));
		
		int aula = 0;
		
		Calendar aula1 = Calendar.getInstance();
		aula1.set(Calendar.HOUR_OF_DAY, 8);
		aula1.set(Calendar.MINUTE, 20);
		aula1.set(Calendar.MILLISECOND, 0);
		
		Calendar aula2 = Calendar.getInstance();
		aula2.set(Calendar.HOUR_OF_DAY, 9);
		aula2.set(Calendar.MINUTE, 10);
		aula2.set(Calendar.MILLISECOND, 0);
		
		Calendar aula3 = Calendar.getInstance();
		aula3.set(Calendar.HOUR_OF_DAY, 10);
		aula3.set(Calendar.MINUTE, 0);
		aula3.set(Calendar.MILLISECOND, 0);
		
		Calendar aula4 = Calendar.getInstance();
		aula4.set(Calendar.HOUR_OF_DAY, 11);
		aula4.set(Calendar.MINUTE, 5);
		aula4.set(Calendar.MILLISECOND, 0);
		
		Calendar aula5 = Calendar.getInstance();
		aula5.set(Calendar.HOUR_OF_DAY, 12);
		aula5.set(Calendar.MINUTE, 0);
		aula5.set(Calendar.MILLISECOND, 0);
		
		agora.set(Calendar.MILLISECOND, 0);
		
		if (agora.getTimeInMillis() <= aula1.getTimeInMillis())
			aula = 1;
		else if (agora.getTimeInMillis() <= aula2.getTimeInMillis())
			aula = 2;
		else if (agora.getTimeInMillis() <= aula3.getTimeInMillis())
			aula = 3;
		else if (agora.getTimeInMillis() <= aula4.getTimeInMillis())
			aula = 4;
		else if (agora.getTimeInMillis() <= aula5.getTimeInMillis())
			aula = 5;
		
		c.add(Restrictions.eq("aula", aula));
		CalendarioSemanal result = (CalendarioSemanal) c.uniqueResult();
		
		Disciplina disciplina = (result != null ? result.getDisciplina() : null);
		return disciplina;
	}
	
	public static void main(String[] args) {
		Calendar agora = Calendar.getInstance();
		int week = agora.get(Calendar.DAY_OF_WEEK);
		
		System.out.println(week);
	}
	
	

}
